﻿/// <reference path="../GeneratedArtifacts/viewModel.js" />
/// <reference path="../Scripts/c1ls-4.0.20141.30.js" />

if (typeof c1ls === "undefined") {
    window.location.href = "http://bit.ly/c1ls-nuget";
}

// http://bit.ly/c1ls-eventscalendar
//
myapp.CalendarTapAction.Games_render = function (element, contentItem) {

    $.wijmo.wijcalendar.prototype.options.wijCSS.stateDefault = "ui-btn-up-a";
    $.wijmo.wijcalendar.prototype.options.wijCSS.content = "ui-body ui-body-a";

    var div = $("<div/>");
    div.appendTo($(element));
    div.attr("style", "width: 800px; height: 640px");

    contentItem.dataBind("value.isLoaded", function (newValue) {
        if (newValue) {
            div.wijevcal({
                timeInterval: 60,
                timeIntervalHeight: 20,
                viewType: "day"
            });

            $(".wijmo-wijev-headerbar .wijmo-wijev-tools").controlgroup("refresh");

            contentItem.value.oncollectionchange = function (a) {
                var calendar = c1ls.getCalendarContent(contentItem), timer;

                function refresh() {
                    if (div.is(":visible")) {
                        clearInterval(timer);
                        div.wijevcal("option", "beforeEditEventDialogShow", calendar.Select);
                        div.wijevcal("option", "eventsData", calendar.Events);
                    }
                }

                if (a.detail.action === "refresh") {
                    timer = setInterval(refresh, 100);
                }
            };

            c1ls.renderControl(contentItem);
        }
    });
};
